var noteswin = "";
function createnotes() {
   noteswin = new Window('notes_dialog', {title:"Notes", top:100, left:250, zIndex:150, resizable: true, showEffect:Element.show, hideEffect: Element.hide, width:600,height:400});
   noteswin.setCookie();
   noteswin.getContent().innerHTML = "Loading....";
   }
createnotes();
function notes(id) {
   noteswin.getContent().innerHTML = "<iframe height=\"99%\" width=\"99%\" name=\"Notes_content\"  id=\"Notes_content\" SRC=\"/notes/show/"+id + "\"> </iframe>"
   noteswin.show();
   }
function disp_del(id) {
   if(id=='-1') {
      alert('Error: Task not found');
      return;
      }
   if(id=='-2') {
      alert('Error: Task can not be deleted');
      return;
      }
   var el = $('de'+id);
   new Effect.Fade(el);
   notice('Task has been deleted');
   var el=$('item_'+id);
   new Effect.BlindUp(el,{delay:1});
   }
function del(id) {
   if (confirm('Delete this task ?\n\"'+$('t'+id).value+'\"')) {
      var el = $('de'+id);
      el.src = '/images/loading.gif';
      new Ajax.Request('/task/delete/'+id, {asynchronous:true, evalScripts:true, onComplete:function(request){disp_del(request.responseText)}});
      }
   return false;
   }
function saved(id,done){
   var el = $('s'+id);
   new Effect.Fade(el);
   el = $('item_'+id);
   var to;
   if(done) to = $('taskdoneul');
   else to = $('taskslist');
   if(el.parentNode != to) to.insertBefore(el,to.firstChild);
   return false;
   }
function save(id,createproject) {
	if($('p'+id).value=='') {
		alert('You must assign this task to a project. Type the name of the project this task belongs to in the first field. If the project does not exists, it will be created.');
		return false;
		}
   if(String(createproject)=="undefined") createproject = 0;
   var el = $('s'+id);
   el.src = '/images/loading.gif';
   new Ajax.Request('/task/save/'+id, {asynchronous:true, evalScripts:true, onComplete:function(request){eval(request.responseText)}, parameters:Form.serialize('item_'+id)+"&C="+createproject});
   return false;
   }
function setclass(c,i){
   $('p'+i).className = c;
   $('t'+i).className = c;
   $('r'+i).className = c;
   $('pr'+i).className = c;
   $('da'+i).className = c;
   }
Array.prototype.ismember = function(i) {
   for(var ind=0; ind < this.length;ind++) if(i==this[ind]) return true;
   return false;
   }
var ignore_keys = new Array('9','13','16','17','18','27','35','36','39','37','38','40','116');
function change(i,event) {
	event = event || window.event;
   if(event!=null) {
		event.preventDefault();	
      var key = event.keyCode;
      //alert(key);
      if(ignore_keys.ismember(key)) return true;
		if(event.ctrlKey && (key=='67')) return true; // Ctr-C
		if(event.ctrlKey && (key=='65')) return true; // Ctr-A
		if(event.ctrlKey && (key=='83')) {save(i);return false;} // Ctr-S, does not work...
      }
   // show save
   var el = $('s'+i);
   el.src = '/images/save.png';
   if(el.style.display == 'none') new Effect.Appear(el);
   // change style if done
   el = $('d'+i);
   if(el.checked) setclass('done',i);
   else setclass('',i);
   return false;
   }
function recreate_sortable(el) {
   Sortable.destroy(el);
   Sortable.create(el, {onUpdate:function(){new Ajax.Request('/task/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("taskslist")})}});
   }
function displaynew(id,str) {
   Element.hide('loading');
   new Insertion.Top('taskslist',str);
   $('p'+id).focus();
   recreate_sortable('taskslist');
   return false;
   }
function add_new_row() {
	Element.show('loading');
   new Ajax.Request('/task/createnew', {asynchronous:true, evalScripts:true, onComplete:function(request){eval(request.responseText)}});
   return false;
   }
function display_tasks(str) {
   var el = $('taskcontent');
   el.innerHTML = str;
   Element.hide('loading');
   recreate_sortable('taskslist');
   return false;
   }
function display_project_resp(str) {
   $('members').innerHTML = str;
   return false;
   }
function select_project(str) {
   if(str=="") return;
   Element.show('loading');
   new Ajax.Request('/task/select_project/'+str, {asynchronous:true, evalScripts:true, onComplete:function(request){display_tasks(request.responseText)}});
   new Ajax.Request('/task/show_project_resp/'+str, {asynchronous:true, evalScripts:true, onComplete:function(request){display_project_resp(request.responseText)}});
   return false;
   }
function select_resp(str) {
   if(str=="") return;
   Element.show('loading');
   new Ajax.Request('/task/select_resp/'+str, {asynchronous:true, evalScripts:true, onComplete:function(request){display_tasks(request.responseText)}});
   return false;
   }
//Mouse over task's element
function mover(e,add) {
   if(String(add) == "undefined") add = "";
   if(add!="") add = "<br /><br /><span>"+add+"</span>";
   switch(e) {
      case 'd' : $('info').innerHTML = "Click to indicate that this task is done"+add; break;
      case 'p' : $('info').innerHTML = "Name of the project this task belongs to"+add; break;
      case 't' : $('info').innerHTML = "Title of the task to do"+add; break;
      case 'r' : $('info').innerHTML = "Responsible of this task"+add; break;
      case 'pr' : $('info').innerHTML = "Priority of this task (number)"+add; break;
      case 's' : $('info').innerHTML = "Save changes"+add; break;
      case 'de' : $('info').innerHTML = "Delete permanently this task"+add; break;
      case 'a' : $('info').innerHTML = "Add a new task"+add; break;
      case 'n' : $('info').innerHTML = "Edit notes"; break;
      case 'dea' : $('info').innerHTML = "Edit deadline"; break;
      case 'da' : $('info').innerHTML = "Task\'s due date"; break;
      case '' :  $('info').innerHTML = ""+add; break;
      default : $('info').innerHTML = "no help for "+e; break;
      }
   }
function create_project_confirm(taskid) {
   saveimg = $('s'+taskid);
   saveimg.src = '/images/save.png';
   var str = "This project does not exist.\nDo you want to create the project named '"+get('p'+taskid).value+"' ?";
   if(confirm(str)) save(taskid,1);
   }
var calid;   
function dispcal2(cal) {
   change(calid,null);
   }   
function dispcal(id,value) {
   calid = id;
   Calendar.setup({
      inputField     :    "da"+id,
      ifFormat       :    "%Y-%m-%d",
      showsTime      :    false,
      onUpdate       :    dispcal2
      });
   }
function star(taskid,value) {
   img = $('st'+taskid);
	switch(value) {
		case 1: str = 'on';break;
		default: str = 'off';break;
		}
   img.src = '/images/star_'+str+'_2.gif';
	}
